﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using CourseBook;

namespace CourseBook
{
    public partial class Staff : System.Web.UI.Page
    {
        
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        SqlConnection con;
        int txtid;
        string str = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;

        protected void Page_Load(object sender, EventArgs e)
        {
            sua.Enabled = false;
            if (!IsPostBack)
            {
                PrintHouse_Load();
               
            }
        }

        private void PrintHouse_Load()
        {
            ds = new DataSet();
            using (con = new SqlConnection(str))
            {
                con.Open();
                cmd = new SqlCommand("Staff_Load", con);
                cmd.CommandType = CommandType.StoredProcedure;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                con.Close();
                gvStaff.DataSource = ds.Tables[0];
                gvStaff.DataBind();
            }
        }

        protected void nhap_click(object sender, EventArgs e)
        {
            using (con = new SqlConnection(str))
            {
                try
                {
                    con.Open();
                    cmd = new SqlCommand("Staff_Insert", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@FamilyName", SqlDbType.NChar).Value = txtFamilyName.Text;
                    cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = txtName.Text;
                    cmd.Parameters.Add("@Note", SqlDbType.NChar).Value = txtNote.Text;
                    cmd.Parameters.Add("@Faculty", SqlDbType.NChar).Value = txtFaculty.Text;
                    cmd.Parameters.Add("@EduLevel", SqlDbType.NVarChar).Value = txtEduLevel.Text;
                    cmd.Parameters.Add("@LearningLevel", SqlDbType.NChar).Value = txtLearningLevel.Text;
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception)
                { }

                PrintHouse_Load();
            }
        }

        protected void refresh_click(object sender, EventArgs e)
        {
            btnRefresh.CausesValidation = true;
            Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
            txtFamilyName.Text = "";
            txtName.Text = "";
            txtNote.Text = "";
            txtFaculty.Text = "";
            txtEduLevel.Text = "";
            txtLearningLevel.Text = "";
        }

        public override void VerifyRenderingInServerForm(Control control)
        {

        }

        protected void sua_click(object sender, EventArgs e)
        {

            int key = (int)gvStaff.DataKeys[gvStaff.SelectedIndex].Value;
            using (con = new SqlConnection(str))
            {
                try
                {
                    con.Open();
                    cmd = new SqlCommand("Staff_Update", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@id", SqlDbType.Int).Value = key;
                    cmd.Parameters.Add("@FamilyName", SqlDbType.NChar).Value = txtFamilyName.Text;
                    cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = txtName.Text;
                    cmd.Parameters.Add("@Note", SqlDbType.NChar).Value = txtNote.Text;
                    cmd.Parameters.Add("@Faculty", SqlDbType.NChar).Value = txtFaculty.Text;
                    cmd.Parameters.Add("@EduLevel", SqlDbType.NVarChar).Value = txtEduLevel.Text;
                    cmd.Parameters.Add("@LearningLevel", SqlDbType.NChar).Value = txtLearningLevel.Text;
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception)
                { }
                PrintHouse_Load();
            }
        }

        protected void xoa_click(object sender, EventArgs e)
        {
            
        }

        protected void Cancel_click(object sender, EventArgs e)
        {
            
        }

        protected void gvStaff_SelectedIndexChanged(object sender, EventArgs e)
        {
            sua.Enabled = true;
            int dong = gvStaff.SelectedIndex;
            txtFamilyName.Text = gvStaff.Rows[dong].Cells[2].Text.ToString();
            txtName.Text = gvStaff.Rows[dong].Cells[3].Text.ToString();
            txtNote.Text = gvStaff.Rows[dong].Cells[4].Text.ToString();
            txtFaculty.Text = gvStaff.Rows[dong].Cells[5].Text.ToString();
            txtEduLevel.Text = gvStaff.Rows[dong].Cells[6].Text.ToString();
            txtLearningLevel.Text = gvStaff.Rows[dong].Cells[7].Text.ToString();
        }

        protected void gvStaff_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int key = (int)gvStaff.DataKeys[e.RowIndex].Value;
            using (con = new SqlConnection(str))
            {
                try
                {
                    con.Open();
                    cmd = new SqlCommand("Staff_Delete", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@id", SqlDbType.Int).Value = key;
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception)
                {

                }

                PrintHouse_Load();
            }
        }
    }
}